from flask import Flask, render_template, redirect, url_for
from flask_cors import CORS
from gevent import monkey
from gevent.pywsgi import WSGIServer
from api.book import book
from api.book_rank import book_rank
from api.book_sort import book_sort
from api.book_store import book_store
from api.download_file import download_file
from api.user import user
from api.book_self import book_self
import config
from exts import db

monkey.patch_all()

app = Flask(__name__)
app.config.from_object(config)
CORS(app)
db.init_app(app)

app.register_blueprint(book_rank)
app.register_blueprint(book)
app.register_blueprint(book_sort)
app.register_blueprint(book_store)
app.register_blueprint(download_file)
app.register_blueprint(user)
app.register_blueprint(book_self)


@app.route('/')
def hello_world():
    return render_template('index.html')


@app.route('/favicon.ico')
def favicon_ico():
    return redirect(url_for("static", filename="image/favicon.ico"))


@app.route('/app/android')
def download_android():
    return redirect(url_for("static", filename="apk/book_v1.0.0.apk"))


if __name__ == '__main__':
    # host = '118.89.16.63'
    # host = 'localhost'
    host = '192.168.1.191'
    app.config.update(debug=True)
    # app.run(host=host, port=5000, debug=True)
    http_server = WSGIServer((host, 5000), app)
    http_server.serve_forever()
